# @time     :2022/1/1
# @Author   :Simple
# @FileName :base_class
import os

import requests
import yaml
from httprunner import HttpRunner


class BaseClass(HttpRunner):
    def __init__(self):
        # 创建一个session，所有请求使用同一个session，给请求提速
        self.s = requests.Session()
        self.token = self.get_token()
        # 共同参数放入session中，session会自动带入请求
        self.s.params = {'access_token': self.token}

    def get_token(self):
        """
        获取企业微信'通讯录'的access_token
        :return:
        """
        url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
        params = self.read_yaml("/../data/corp.yaml")
        r = self.s.get(url, params=params)
        return r.json()['access_token']

    def send(self, *args, **kwargs):
        return self.s.request(*args, **kwargs)

    def read_yaml(self, datapath):
        file_path = os.path.dirname(__file__)
        with open(file_path + datapath, encoding='utf-8') as file:
            content = yaml.load(file.read())
            return content


# if __name__ == "__main__":
#     base = BaseClass()
#     print(base.get_token())
